package com.novellotus.iov.carvice.activity;

import android.app.Activity;
import android.gesture.Gesture;
import android.gesture.GestureOverlayView;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.MotionEvent;

import com.novellotus.iov.carvice.R;
import com.novellotus.iov.carvice.model.AutographModel;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 签名页面
 */
public class AutographActivity extends Activity implements GestureOverlayView.OnGesturePerformedListener, GestureOverlayView.OnGesturingListener, GestureOverlayView.OnGestureListener {

    @BindView(R.id.gestureOverlayView)
    GestureOverlayView gestureOverlayView;
    private Gesture gesture;
    private AutographModel autographModel;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_autograph);
        ButterKnife.bind(this);
        initViews();
    }


    private void initViews() {
        autographModel = new AutographModel(this);
        // 设置手势可多笔画绘制，默认情况为单笔画绘制
        gestureOverlayView.setGestureStrokeType(GestureOverlayView.GESTURE_STROKE_TYPE_MULTIPLE);
        // 设置手势的颜色)
        gestureOverlayView.setGestureColor(getResources().getColor(R.color.text_black));
        // 设置还没未能形成手势绘制是的颜色(红色)
        gestureOverlayView.setUncertainGestureColor(getResources().getColor(R.color.text_black));
        // 设置手势的粗细
        gestureOverlayView.setGestureStrokeWidth(20f);
        /*
         * 手势绘制完成后淡出屏幕的时间间隔，即绘制完手指离开屏幕后相隔多长时间手势从屏幕上消失；
		 * 可以理解为手势绘制完成手指离开屏幕后到调用onGesturePerformed的时间间隔 默认值为420毫秒，这里设置为2秒
		 */
        gestureOverlayView.setFadeOffset(3600000);
        gestureOverlayView.setSaveEnabled(true);

        // 绑定监听器
        gestureOverlayView.addOnGesturePerformedListener(this);
        gestureOverlayView.addOnGesturingListener(this);
        gestureOverlayView.addOnGestureListener(this);

    }

    @OnClick(R.id.img_back)
    void back() {
        finish();
    }

    @OnClick(R.id.img_clear)
    void clear() {
        autographModel.clear(gestureOverlayView);
    }

    @OnClick(R.id.img_finish)
    void finishSign() {
        autographModel.confirmSign(gesture);
    }

    @Override
    public void onGestureStarted(GestureOverlayView gestureOverlayView, MotionEvent motionEvent) {
        gesture = null;
    }

    @Override
    public void onGesture(GestureOverlayView gestureOverlayView, MotionEvent motionEvent) {

    }

    @Override
    public void onGestureEnded(GestureOverlayView gestureOverlayView, MotionEvent motionEvent) {
        gesture = gestureOverlayView.getGesture();
        if (gesture != null) {
            autographModel.hasSign = true;
        }
    }

    @Override
    public void onGestureCancelled(GestureOverlayView gestureOverlayView, MotionEvent motionEvent) {

    }

    @Override
    public void onGesturePerformed(GestureOverlayView gestureOverlayView, Gesture gesture) {

    }

    @Override
    public void onGesturingStarted(GestureOverlayView gestureOverlayView) {

    }

    @Override
    public void onGesturingEnded(GestureOverlayView gestureOverlayView) {

    }
}
